package com.wd.gui;

import com.wd.dominio.Empleado;
import com.wd.dominio.HistorialEmpleado;
import com.wd.dominio.Tienda;
import com.wd.gui.controlparticular.ControlGuiEmpleadoTienda;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Gabylis
 */
public class VentanaTransferenciaEmpTiendas extends javax.swing.JFrame {

    private Vector<Tienda> vecTiendas;

    private ControlGuiEmpleadoTienda controlEmpTienda = new ControlGuiEmpleadoTienda();;

    private Vector<Empleado> vecEmpTienda;

    private DefaultTableModel dm;

    private Vector<HistorialEmpleado> vecHistorial;

    private ControlGui controlGeneral;


    /** Creates new form VentanaTransferenciaEmpTiendas */
    public VentanaTransferenciaEmpTiendas(Vector<Tienda> result) {
        initComponents();

        vecEmpTienda = new Vector();

        vecTiendas = new Vector();
        vecTiendas = result;                

        for (Tienda tienda : result) {
            this.comboTiendaOrigen.addItem(tienda.getNombre());
            this.comboTiendaDestino.addItem(tienda.getNombre());
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        comboTiendaOrigen = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableEmpsOr = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        comboTiendaDestino = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        comboCargo = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Tranferencia de Empleados entre Tiendas");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Transferencia de Empleados entre Tiendas", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tienda Origen", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N
        jPanel2.setToolTipText("Escoja la tienda en donde se encuentra el empleado a transferir");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Tiendas");

        comboTiendaOrigen.setToolTipText("Tienda En donde se encuentra el empleado a transferir");
        comboTiendaOrigen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboTiendaOrigenActionPerformed(evt);
            }
        });

        tableEmpsOr.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        tableEmpsOr.setToolTipText("Seleccione el empleado y luego presione el botón transferir");
        jScrollPane1.setViewportView(tableEmpsOr);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel2)
                        .addGap(154, 154, 154)
                        .addComponent(comboTiendaOrigen, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(comboTiendaOrigen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tienda destino", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N
        jPanel3.setToolTipText("Escoja la informacion de la tienda a donde desea tranferir el empleado");

        comboTiendaDestino.setToolTipText("Tienda a donde se desea tranferir el empleado");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setText("Tiendas");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("Cargo");
        jLabel4.setToolTipText("Cargo que el empleado ocupara en la tienda");

        comboCargo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Gerente", "Regular" }));
        comboCargo.setToolTipText("Escoja el cargo que ocupara el empleado en la tienda");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3))
                .addGap(164, 164, 164)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(comboCargo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(comboTiendaDestino, 0, 211, Short.MAX_VALUE))
                .addContainerGap(155, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(comboTiendaDestino, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(comboCargo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton1.setText("Tranferir Empleado");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel1))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(186, 186, 186)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboTiendaOrigenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboTiendaOrigenActionPerformed
        int select = -1;
        Integer codigoTienda = -1;
        select = this.comboTiendaOrigen.getSelectedIndex();
        codigoTienda = this.vecTiendas.elementAt(select).getCodigo();
        this.llenarTablaEmps(codigoTienda);
    }//GEN-LAST:event_comboTiendaOrigenActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int selectTiendaDestino = -1;
        int selectCargo = -1;
        int selectTiendaOri = -1;
        int resultado = -1;
        int selectEmp = -1;
        boolean flag = true;
        int cedula =-1;
        String nombre="";
        String apellido="";
        java.sql.Date fechaNacimiento = null;
        String telefono = "";
        int estadoCivil = -1;
        String sexo = "";
        int nivelEstudios = -1;
        String direccion = "";
        int tipo = -1;
        int lugarId=-1;
        String ciudadVive = "";
        int codigo = -1;
        String nombreEmpresa="";
        Empleado emp = new Empleado();
        this.controlGeneral =  new ControlGui ();
        resultado = this.controlGeneral.dialogoConfirmacion("¿Está seguro " +
        "que desea realizar esta operación?");
        if (resultado == 0){
            selectTiendaDestino = this.comboTiendaDestino.getSelectedIndex();
            codigo = this.vecTiendas.elementAt(selectTiendaDestino).getCodigo();
            selectCargo = this.comboCargo.getSelectedIndex();
            selectEmp = this.tableEmpsOr.getSelectedRow();
            emp = this.vecEmpTienda.elementAt(selectEmp);

            cedula = emp.getCedula();
            nombre = emp.getNombre();
            apellido = emp.getApellido();
            fechaNacimiento = emp.getFechaNacimiento();
            telefono = emp.getTelefono();
            estadoCivil = emp.getEstadoCivil();
            sexo = emp.getSexo();
            nivelEstudios = emp.getNivelEstudios();
            direccion = emp.getDireccion();
            tipo = selectCargo;
            lugarId = emp.getLugarId();
            ciudadVive = emp.getCiudadVive();
            nombreEmpresa = this.vecTiendas.elementAt(selectTiendaDestino).getNombre();

            this.controlEmpTienda.actualizarEmpleado(flag, cedula, nombre,
            apellido, fechaNacimiento, telefono, estadoCivil, sexo, nivelEstudios,
            direccion, tipo, lugarId, ciudadVive, codigo, nombreEmpresa);
        }
        //this.dm.removeRow(selectEmp);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run(Vector<Tienda> result) {
                new VentanaTransferenciaEmpTiendas(result).setVisible(true);
            }
            public void run() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox comboCargo;
    private javax.swing.JComboBox comboTiendaDestino;
    private javax.swing.JComboBox comboTiendaOrigen;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableEmpsOr;
    // End of variables declaration//GEN-END:variables

    public void llenarTablaEmps(Integer codigoTienda){
        
        this.vecEmpTienda = controlEmpTienda.traerTodosEmpTiendas(codigoTienda);
        
        dm = new DefaultTableModel();
        dm.addColumn("CI");
        dm.addColumn("Nombre");
        dm.addColumn("Apellido");
        dm.addColumn("Sexo");
        dm.addColumn("Fecha Nacimiento");
        dm.addColumn("Telefono");
        dm.addColumn("Cargo");

        for (Empleado emp : vecEmpTienda){
            Vector info = new Vector();
            info.addElement(emp.getCedula());
            info.addElement(emp.getNombre());
            info.addElement(emp.getApellido());
            info.addElement(emp.getSexo());
            info.addElement(emp.getFechaNacimiento());
            info.addElement(emp.getTelefono());
            if (emp.getTipo()==0){
                info.addElement("Gerente");
            }else if (emp.getTipo()==1){
                info.addElement("Empleado Regular");
            }
            dm.addRow(info);
        }
        this.tableEmpsOr.setModel(dm);
    }
}
